package com.spider.spiderorderadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spider.spiderorderadmin.common.Result;
import com.spider.spiderorderadmin.common.annotation.OperationAspect;
import com.spider.spiderorderadmin.entity.dto.RoleResourceDTO;
import com.spider.spiderorderadmin.entity.model.SystemRole;
import com.spider.spiderorderadmin.entity.model.User;
import com.spider.spiderorderadmin.entity.param.SystemRoleQueryParam;
import com.spider.spiderorderadmin.service.SystemRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author xiong.bo
 * @version 1.0
 * @date 2020/11/21 9:49
 */

@Api(tags = "角色模块")
@RequestMapping("/system/role")
@RestController
public class SystemRoleController extends AbstractController {

    @Resource
    private SystemRoleService systemRoleService;

    @ApiOperation("角色列表")
    @GetMapping
    @OperationAspect(accessPath = "/system/role/selectPage",accessDesc = "角色列表")
    public Result<Page<SystemRole>> selectPage(SystemRoleQueryParam queryParam) {
        return Result.of(systemRoleService.selectPage(queryParam));
    }

    @ApiOperation("启用状态的角色列表")
    @GetMapping("/activeList")
    @OperationAspect(accessPath = "/system/role/activeList",accessDesc = "启用状态的角色列表")
    public Result<Page<SystemRole>> selectActivePage(SystemRoleQueryParam queryParam) {
        return Result.of(systemRoleService.selectActivePage(queryParam));
    }

    @ApiOperation("角色详情")
    @GetMapping("/{id}")
    @OperationAspect(accessPath = "/system/role/findById",accessDesc = "角色详情")
    public Result<SystemRole> findById(@PathVariable Long id) {
        return Result.of(systemRoleService.findById(id));
    }

    @ApiOperation("添加角色")
    @PostMapping
    @OperationAspect(accessPath = "/system/role/saveRole",accessDesc = "删除角色")
    public Result<SystemRole> saveRole(@RequestBody SystemRole role) {
        return Result.of(systemRoleService.saveRole(role, super.getUserContentId()));
    }

    @ApiOperation("修改角色")
    @PutMapping
    @OperationAspect(accessPath = "/system/role/updateRole",accessDesc = "删除角色")
    public Result<SystemRole> updateRole(@RequestBody SystemRole role) {
        return Result.of(systemRoleService.updateRole(role, super.getUserContentId()));
    }

    @ApiOperation("角色的启用禁用")
    @PostMapping("/updateStatus/{id}")
    @OperationAspect(accessPath = "/system/role/updateStatus", accessDesc = "角色的启用禁用")
    public Result<Boolean> updateStatus(@PathVariable Long id, @RequestParam("status") Integer status) {
        return Result.of(systemRoleService.updateStatus(id, status));
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delete")
    @OperationAspect(accessPath = "/system/role/delete",accessDesc = "删除角色")
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.of(systemRoleService.deleteRole(ids));
    }

    @ApiOperation("角色绑定资源的列表")
    @GetMapping("/resource/{id}")
    @OperationAspect(accessPath = "/system/role/findResourceByRole",accessDesc = "角色绑定资源的列表")
    public Result<List<Long>> findResourceByRole(@PathVariable Long id) {
        return Result.of(systemRoleService.findResourceByRole(id));
    }

    @ApiOperation("给角色绑定资源,资源多选")
    @PostMapping("/bindingResource")
    @OperationAspect(accessPath = "/system/role/bindingResource", accessDesc = "给角色绑定资源")
    public Result<Boolean> bindingRole(@RequestBody RoleResourceDTO dto) {
        return Result.of(systemRoleService.bindingRole(dto));
    }

    @ApiOperation("查看单个角色关联的用户列表")
    @GetMapping("/relUsers")
    @OperationAspect(accessPath = "/system/role/relUsers",accessDesc = "查看单个角色关联的用户列表")
    public Result<Page<User>> findUsersByRole(SystemRoleQueryParam queryParam) {
        return Result.of(systemRoleService.findUsersByRole(queryParam));
    }

}
